from django.db import models
from common.model_mixin import ModelMixin


class User(models.Model, ModelMixin):
    role_id = models.CharField(max_length=200)
    user_name = models.CharField(max_length=100, unique=True)
    user_pwd = models.CharField(max_length=200, blank=True, null=True)
    key = models.CharField(max_length=20, blank=True, null=True)
    phone_number = models.CharField(max_length=11, blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    enable = models.BooleanField(default=True)
    gender = models.BooleanField(default=True)    # 1: 男 0：女
    token_str = models.TextField(blank=True, null=True)
    remark = models.CharField(max_length=200, blank=True, null=True)
    is_archive = models.IntegerField(default=1)  # 1: active  2: archive
    create_at = models.DateTimeField(auto_now_add=True)
    creator = models.CharField(max_length=100, blank=True, null=True)
    update_at = models.DateTimeField(auto_now=True)
    updater = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user'

    def to_dict(self):
        return {
            'id': self.id,
            'role_id': self.role_id,
            'user_name': self.user_name,
            # 'key': self.key,
            'phone_number': self.phone_number,
            'email': self.email,
            'enable': self.enable,
            'gender': self.gender,
            'token_str': self.token_str,
            'remark': self.remark,
            'creator': self.creator,
            'updater': self.updater,
            'create_at': self.create_at.strftime('%Y-%m-%d %H:%M:%S'),
            'update_at': self.update_at.strftime('%Y-%m-%d %H:%M:%S'),
        }

    def to_user_name(self):
        return {
            "label": self.user_name,
            "value": self.id
        }


class Role(models.Model):
    name = models.CharField(unique=True, max_length=45, blank=True, null=True)
    intro = models.CharField(max_length=45, blank=True, null=True)
    menu_list = models.CharField(max_length=1024, blank=True, null=True)
    remark = models.CharField(max_length=200, blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(auto_now=True)
    creator = models.CharField(max_length=45, blank=True, null=True)
    updater = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'role'

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'intro': self.intro,
            'menu_list': Menu.make_menu_tree([i.to_dict() for i in Menu.objects.filter(pk__in=eval(self.menu_list))]),
            'remark': self.remark,
            'creator': self.creator,
            'updater': self.updater,
            'create_at': self.create_at.strftime('%Y-%m-%d %H:%M:%S'),
            'update_at': self.update_at.strftime('%Y-%m-%d %H:%M:%S')
        }

    @property
    def get_menu_list(self):
        return eval(self.menu_list)


class Menu(models.Model, ModelMixin):
    name = models.CharField(max_length=45, blank=True, null=True)
    type = models.IntegerField()    # 1: 1级菜单 2：2级菜单
    icon = models.CharField(max_length=45, blank=True, null=True)
    parent_id = models.IntegerField()
    url = models.CharField(max_length=45, blank=True, null=True)
    sort = models.IntegerField()
    permission = models.CharField(max_length=100, blank=True, null=True)
    remark = models.CharField(max_length=200, blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(blank=True, null=True)
    creator = models.CharField(max_length=45, blank=True, null=True)
    updater = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'menu'

    # def to_dict(self):
    #     return {
    #         'id': self.id,
    #         'name': self.name,
    #         'type': self.type,
    #         'icon': self.icon,
    #         'parent_id': self.parent_id,
    #         'url': self.url,
    #         'sort': self.sort,
    #         'permission': self.permission,
    #         'remark': self.remark,
    #         'creator': self.creator,
    #         'updater': self.updater,
    #         'create_at': self.create_at.strftime('%Y-%m-%d %H:%M:%S') if self.create_at else self.create_at,
    #         'update_at': self.update_at.strftime('%Y-%m-%d %H:%M:%S') if self.update_at else self.update_at
    #     }

    @staticmethod
    def make_menu_tree(menu_list):
        tree = [node for node in menu_list if node['parent_id'] == 0]
        # print(tree)
        for menu in menu_list:
            if menu["type"] != 3:
                menu["children"] = [n for n in menu_list if menu["id"] == n["parent_id"]]
        # print(tree)
        return tree


class Permission(models.Model, ModelMixin):
    name = models.CharField(max_length=64, blank=True, null=True)
    remark = models.CharField(max_length=200, blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(blank=True, null=True)
    creator = models.CharField(max_length=45, blank=True, null=True)
    updater = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'permission'

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'remark': self.remark,
            'creator': self.creator,
            'updater': self.updater,
            'create_at': self.create_at.strftime('%Y-%m-%d %H:%M:%S'),
            'update_at': self.update_at.strftime('%Y-%m-%d %H:%M:%S') if self.update_at else self.update_at
        }

    def to_permission_name(self):
        return {
            "label": self.name,
            "value": self.id
        }


class Requested(models.Model):
    user_id = models.IntegerField(blank=True, null=True)
    permission_id = models.IntegerField(blank=True, null=True)
    status = models.IntegerField(default=1)     # 1: pending 2: approved
    approver = models.CharField(max_length=45, blank=True, null=True)
    remark = models.CharField(max_length=200, blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(blank=True, null=True)
    creator = models.CharField(max_length=45, blank=True, null=True)
    updater = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'requested'

    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'user_name': User.objects.get(id=self.user_id).user_name,
            'user_email': User.objects.get(id=self.user_id).email,
            'user_phone': User.objects.get(id=self.user_id).phone_number,
            'permission_name': Permission.objects.get(id=self.permission_id).name,
            'status': self.status,
            'approver': self.approver,
            'remark': self.remark,
            'creator': self.creator,
            'updater': self.updater,
            'create_at': self.create_at.strftime('%Y-%m-%d'),
            'update_at': self.update_at.strftime('%Y-%m-%d') if self.update_at else ""
        }

    def to_export_dict(self):
        return {
            'user_name': User.objects.get(id=self.user_id).user_name,
            'user_email': User.objects.get(id=self.user_id).email,
            'user_phone': User.objects.get(id=self.user_id).phone_number,
            'permission_name': Permission.objects.get(id=self.permission_id).name,
            'status': 'pending' if self.status == 1 else 'approved',
            'requested': self.create_at.strftime('%Y-%m-%d'),
            'approver': self.approver
        }


class Record(models.Model, ModelMixin):
    user_id = models.IntegerField(default=1)
    request_url = models.CharField(max_length=64, default='')
    request_type = models.CharField(max_length=64, default='')
    request_host = models.CharField(max_length=64, default='')
    remark = models.CharField(max_length=200, default='')
    request_at = models.DateTimeField(auto_now_add=True)
    login_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'record'

    def to_dict(self):
        return {
            'id': self.id,
            'user_email': User.objects.get(id=self.user_id).email,
            'user_name': User.objects.get(id=self.user_id).user_name,
            'request_url': self.request_url,
            'request_type': self.request_type,
            'login_at': self.login_at.strftime('%Y-%m-%d %H:%M:%S') if self.login_at else "",
            'request_at': self.request_at.strftime('%Y-%m-%d %H:%M:%S'),
        }
